﻿Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class WorkflowTimer
        Implements IDisposable
        ' Methods
        Private Sub New()
            Me.timer.Interval = 50
            AddHandler Me.timer.Tick, New EventHandler(AddressOf Me.OnTimer)
            Me.timer.Stop
        End Sub

        Public Sub Dispose()
            Me.Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Private Sub Dispose(ByVal disposing As Boolean)
            If (Not Me.timer Is Nothing) Then
                If Me.timer.Enabled Then
                    Me.timer.Stop
                End If
                Me.timer.Dispose
                Me.timer = Nothing
            End If
        End Sub

        Protected Overrides Sub Finalize()
            Try 
                Me.Dispose(False)
            Finally
                MyBase.Finalize
            End Try
        End Sub

        Private Sub OnTimer(ByVal sender As Object, ByVal e As EventArgs)
            Dim list As New List([Of] ElapsedEventUnit)(Me.elapsedEvents)
            Dim unit As ElapsedEventUnit
            For Each unit In list
                unit.elapsedTime += 1
                If (unit.elapsedInterval <= unit.elapsedTime) Then
                    unit.elapsedTime = 0
                    unit.elapsedEventHandler.Invoke(Me, EventArgs.Empty)
                End If
            Next
        End Sub

        Friend Sub Subscribe(ByVal elapsedInterval As Integer, ByVal elapsedEventHandler As EventHandler)
            Me.elapsedEvents.Add(New ElapsedEventUnit((elapsedInterval / 50), elapsedEventHandler))
            If Not Me.timer.Enabled Then
                Me.timer.Start
            End If
        End Sub

        Friend Sub Unsubscribe(ByVal elapsedEventHandler As EventHandler)
            Dim list As New List([Of] ElapsedEventUnit)
            Dim unit As ElapsedEventUnit
            For Each unit In Me.elapsedEvents
                If (unit.elapsedEventHandler Is elapsedEventHandler) Then
                    list.Add(unit)
                End If
            Next
            Dim unit2 As ElapsedEventUnit
            For Each unit2 In list
                Me.elapsedEvents.Remove(unit2)
            Next
            If ((Me.elapsedEvents.Count = 0) AndAlso Me.timer.Enabled) Then
                Me.timer.Stop
            End If
        End Sub


        ' Properties
        Friend Shared ReadOnly Property [Default] As WorkflowTimer
            Get
                If (WorkflowTimer.workflowTimer Is Nothing) Then
                    WorkflowTimer.workflowTimer = New WorkflowTimer
                End If
                Return WorkflowTimer.workflowTimer
            End Get
        End Property


        ' Fields
        Private elapsedEvents As List([Of] ElapsedEventUnit) = New List([Of] ElapsedEventUnit)
        Private timer As Timer = New Timer
        Private Const TimerInterval As Integer = 50
        Private Shared workflowTimer As WorkflowTimer

        ' Nested Types
        Private NotInheritable Class ElapsedEventUnit
            ' Methods
            Friend Sub New(ByVal interval As Integer, ByVal eventHandler As EventHandler)
                Me.elapsedInterval = interval
                Me.elapsedEventHandler = eventHandler
            End Sub


            ' Fields
            Friend elapsedEventHandler As EventHandler
            Friend elapsedInterval As Integer
            Friend elapsedTime As Integer
        End Class
    End Class
End Namespace

